<?php

namespace falcon5\inc\Base;

class EditModeConfig
{
    private $params;

    public function __construct()
    {
        $this->params = [
            'key' => Common::getLanguageEditKey(),
            'token' => Common::getLanguageEditToken(),
            'site_id' => Common::getLanguageEditSiteId(),
        ];
    }

    /**
     * 检查是否启用编辑模式
     *
     * @param array $queryParams
     * @return bool
     */
    public function isEditModeEnabled(array $queryParams): bool
    {
        return isset($queryParams[$this->params['key']]) && $queryParams[$this->params['key']];
    }

    /**
     * 获取编辑模式Token
     *
     * @param array $queryParams
     * @return string|null
     */
    public function getEditToken(array $queryParams)
    {
        return $queryParams[$this->params['token']] ?? null;
    }

    /**
     * 获取编辑模式站点ID
     *
     * @param array $queryParams
     * @return string|null
     */
    public function getEditSiteId(array $queryParams)
    {
        return $queryParams[$this->params['site_id']] ?? null;
    }

    /**
     * 清除编辑模式参数
     *
     * @param array $queryParams
     * @return array
     */
    public function clearEditModeParams(array &$queryParams): array
    {
        foreach ($this->params as $param) {
            unset($queryParams[$param]);
        }
        return $queryParams;
    }

    /**
     * 校验编辑模式所需的所有参数
     *
     * @param array $queryParams
     * @return bool
     */
    public function validateEditModeParams(array $queryParams): bool
    {
        if (!$this->isEditModeEnabled($queryParams)) {
            return false;
        }

        $token = $this->getEditToken($queryParams);
        $siteId = $this->getEditSiteId($queryParams);

        return !empty($token) && !empty($siteId);
    }
}